package com.example.designpatterns.interpreter.nonterminal;

import com.example.designpatterns.interpreter.Expression;
import java.util.Map;

/**
 * 条件表达式 - 非终结符表达式
 * 表示 if-then-else 条件: condition ? trueExp : falseExp
 */
public class ConditionExpression implements Expression {
    private Expression condition;
    private Expression trueExpression;
    private Expression falseExpression;

    public ConditionExpression(Expression condition, Expression trueExpression, Expression falseExpression) {
        this.condition = condition;
        this.trueExpression = trueExpression;
        this.falseExpression = falseExpression;
    }

    @Override
    public int interpret(Map<String, Integer> context) {
        // 如果条件结果为非零，执行trueExpression，否则执行falseExpression
        if (condition.interpret(context) != 0) {
            return trueExpression.interpret(context);
        } else {
            return falseExpression.interpret(context);
        }
    }
    
    @Override
    public String toString() {
        return "(" + condition + " ? " + trueExpression + " : " + falseExpression + ")";
    }
} 